package com.example.boot.rabbitmq.provider.controller;

import lombok.AllArgsConstructor;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;
import java.util.Random;

import static com.example.boot.rabbitmq.provider.common.MQConstant.EXCHANGE_NAME;
import static com.example.boot.rabbitmq.provider.common.MQConstant.ROUTING_KEY;

@RestController
@AllArgsConstructor
@RequestMapping("/message")
public class SendMessageController {

    // 使用 RabbitTemplate，这提供了接收/发送等等方法
    private final RabbitTemplate rabbitTemplate;

    @GetMapping("/send")
    public String send(String phone) {
        Map<String, String> phoneMap = new HashMap<>();
        // 生成 6 位随机数字验证码
        String code = String.valueOf(new Random().nextDouble()).substring(12);
        phoneMap.put("phone", phone);
        phoneMap.put("code", code);
        rabbitTemplate.convertAndSend(EXCHANGE_NAME, ROUTING_KEY, phoneMap);
        return "boot-rabbitmq-provider 发送消息：" + phoneMap;
    }

}
